package com.maijinjie.common.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.maijinjie.common.bean.Pagination;
import com.maijinjie.common.bean.StatusBean;
import com.maijinjie.common.dao.BaseDAO;
/**
 * 服务基本类
 * @version 1.0
 * @author GhostarX ( 王欣逸 ) 100992995@qq.com
 * @data 2013-8-12 下午2:35:38
 */


public class BaseServiceImpl {
	/** 日志类 */
	public Logger log = Logger.getLogger(this.getClass());
	/**
	 * 获取分页对象
	 * @param dao
	 * @param pagination
	 * @return
	 * @throws Exception
	 * @version 1.0
	 * @author GhostarX ( 王欣逸 ) 100992995@qq.com 18902499340
	 * @data 2013-8-13 上午11:28:36
	 */
	public Pagination<?> getPagination(BaseDAO dao, Pagination pagination) throws Exception{
		if(null!=pagination){
			pagination.setRowCount(dao.count(pagination.getParamMap()));
			pagination.setParam("startRow", pagination.getStartRow());
			pagination.setParam("rows", pagination.getRows());
			pagination.setList(dao.list(pagination.getParamMap()));
		}
		return pagination;
	}
	/**
	 * 通过ID获取bean
	 * @param dao
	 * @param id
	 * @return Object
	 * @throws Exception
	 * @version 1.0
	 * @author GhostarX ( 王欣逸 ) 100992995@qq.com 18902499340
	 * @data 2013-8-12 下午2:54:16
	 */
	public Object getBeanById(BaseDAO dao, int id) throws Exception{
		Object obj = null;
		if(0<id){
			Map<String, Object> params=new HashMap<String, Object>();
			params.put("id", id);
			obj = getFirstOneByList(dao.list(params));
		}
		return obj;
	}
	/**
	 * 从List中获取第一个元素
	 * @param list
	 * @return Object
	 */
	public Object getFirstOneByList(List<?> list){
		Object obj = null;
		if(list!=null && list.size()>0){
			obj = list.get(0);
		}
		return obj;
	}
	/**
	 * 操作成功
	 * @param msg 消息
	 * @return StatusBean
	 */
	public StatusBean success(String msg){
		return new StatusBean(true,msg);
	}
	/**
	 * 操作成功
	 * @param msg 消息
	 * @return StatusBean
	 */
	public StatusBean success(String msg, Object result){
		return new StatusBean(true,msg,result);
	}
	/**
	 * 操作失败
	 * @param msg 消息
	 * @return StatusBean
	 */
	public StatusBean error(String msg){
		return new StatusBean(false,msg);
	}
	
}
